<?php
namespace app\service\sys;

use app\model\sys\JwtModel;
use app\model\sys\JwtLogModel;
use app\model\sys\PhoneCodeModel;
use think\facade\Db;

/**
 * 用户行为记录
 *
 * @package app\common\service
 */
class UserBehaviorService
{
    /**
     * 记录创建token
     * 
     * @param string $jwt: token字符串。 [必填]
     * @param array $token: token内容数组。 [必填]
     * @param int $expireTime: 过期时间戳。 [必填]
     * @param array $data: 用户信息数组。 [必填]
     * @param string $table : 用户表名。 [必填]
     * @return boolean: 返回记录入库状态
     * @package app\common\service
     */
    public static function logCreateToken($jwt, $token, $expireTime, $data, $table): object
    {
        $data_id = 0;
        if ($table == 'xh_siyu_sys_member') {
            $data_id = $data['uid'];
        }
        $log = [
            'token' => $jwt,
            'ip' => ToolService::getRealClientIP(),
            'table' => $table,
            'data_id' => $data_id,
            'token_data' => $token,
            'create_time' => date('Y-m-d H:i:s'),
            'expire_time' => date('Y-m-d H:i:s', $expireTime),
        ];
        return JwtModel::create($log);
    }
    
    public static function logLogoutToken()
    {
        $token = trim( ltrim( request()->header('Authorization'), 'Bearer' ) );
        return JwtModel::where('token', $token)->update(['expire_time'=>date('Y-m-d H:i:s')]);
    }
    
    /**
     * 记录插入数据
     * */
    public static function logInsert($id, $data, $table): object
    {
        $user_id = 0;
        if ($table == 'xh_siyu_sys_member') {
            $user_id = $data['uid'];
        }
        $urlArray = ToolService::getCurrentUrl(true);
        $log = [
            'jwt_id' => $id,
            'is_first' => 0,
            'table' => $table,
            'user_id' => $user_id,
            'ip' => ToolService::getRealClientIP(),
            'scheme' => $urlArray['scheme'],
            'host' => $urlArray['host'],
            'uri' => $urlArray['uri'],
            'url' => $urlArray['url'],
            'create_time' => date('Y-m-d H:i:s'),
        ];
        return JwtLogModel::create($log);
    }
    
    /**
     * 生成验证码
     * @param string $phone: 手机号码。 [必填]
     * @param string $code: 验证码。 [必填]
     * @return boolean: 返回记录入库状态
     * */
    public static function generatePhoneCode($phone, $code): object
    {
        $times = time();
        $dateTime = date('Y-m-d H:i:s', $times);
        $expireTime = date('Y-m-d H:i:s', $times + 300);
        $urlArray = ToolService::getCurrentUrl(true);
        $log = [
            'phone' => $phone,
            'code' => $code,
            'gene_time' => $dateTime,
            'gene_ip' => ToolService::getRealClientIP(),
            'gene_scheme' => $urlArray['scheme'],
            'gene_host' => $urlArray['host'],
            'gene_uri' => $urlArray['uri'],
            'gene_url' => $urlArray['url'],
            'expire_time' => $expireTime,
        ];
        return PhoneCodeModel::create($log);
        
    }
    
    /**
     * 使用验证码
     * @param string $id: 验证码ID。 [必填]
     * @return boolean: 返回记录入库状态
     * */
    public static function usePhoneCode($id): bool
    {
        if (!$id) {
            return false;
        }
        $dateTime = date('Y-m-d H:i:s');
        $ip = ToolService::getRealClientIP();
        $urlArray = ToolService::getCurrentUrl(true);
        $log = [
            'use_code' => 1,
            'use_time' => $dateTime,
            'use_ip' => $ip,
            'use_scheme' => $urlArray['scheme'],
            'use_host' => $urlArray['host'],
            'use_uri' => $urlArray['uri'],
            'use_url' => $urlArray['url'],
        ];
        return PhoneCodeModel::where('id', $id)->update($log);
    }
    
    
}